//index.js
const app = getApp()

Page({
  data: {
    signtext: '点击签到',
    days: ['第一天', '第二天', '第三天', '第四天', '第五天', '第六天', '第七天', '第八天', '第九天', '第十天'],
    signIcon: 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/sign.png',
    nosignIcon: 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/nosign.png',
    cardfront: ['http://sd.offcn.com/zg/wximg/2021/jnpt-img/img1.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img2.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img3.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img4.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img5.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img6.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img7.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img8.png', 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/img9.png', ],
    cardback: 'http://sd.offcn.com/zg/wximg/2021/jnpt-img/imgback.png',
    theSwiper: [{
      src: 'http://sd.offcn.com/zg/wximg/2021skhz-img/banner111.png',
      tomini: 'toyy'
    }, {
      src: 'http://sd.offcn.com/zg/wximg/2021skhz-img/banner222.png',
      tomini: 'tot2'
    }],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    page: 74
  },

  onLoad: function (options) {
    var that = this;

    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent,
      page: options.page,
    })
    if (options.p_open_id) {
      console.log('分享')
      that.setData({
        p_open_id: options.p_open_id
      })
    } else {
      console.log('no分享')
    }
    wx.login({
      success(res) {
        console.log(res)
        if (res.code) {
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/wx_login',
            method: 'POST',
            data: {
              code: res.code,
              p_open_id: that.data.p_open_id
            },
            success: res => {
              console.log(res)
              wx.setStorageSync('openId', res.data.user_info.openId)
              console.log(res.data.user_info);
              var cardList = res.data.user_info.cardList
              if (cardList[0] == 1 && cardList[1] == 1 && cardList[2] == 1 && cardList[3] == 1 && cardList[4] == 1 && cardList[5] == 1 && cardList[6] == 1 && cardList[7] == 1 && cardList[8] == 1) {
                that.setData({
                  add: 1
                })
              }
              if (res.data.user_info.times) {
                that.setData({
                  times: res.data.user_info.times
                })
              } else {
                that.setData({
                  times: 0
                })
              }
              that.setData({
                p_open_id: res.data.user_info.openId,
                openId: res.data.user_info.openId,
                cardList: res.data.user_info.cardList,
                cardNum: res.data.user_info.cardNum,
                signNum: res.data.user_info.signNum,
                signTime: res.data.user_info.signTime,
                shareTimes: res.data.user_info.shareTimes,
                answerTime: res.data.user_info.answerTime,
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  // loginIn() {
  //   var that = this;

  // },

  getCard() {
    var that = this;
    var phone = wx.getStorageSync('phone')
    if (phone) {
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/get_card',
        method: 'POST',
        data: {
          openId: that.data.openId
        },
        success: res => {
          console.log(res);

          if (res.data.code == 1) {
            that.setData({
              openId: res.data.user_info.openId,
              cardList: res.data.user_info.cardList,
              cardNum: res.data.user_info.cardNum,
              signNum: res.data.user_info.signNum,
              signTime: res.data.user_info.signTime,
              times: res.data.user_info.times,
              shareTimes: res.data.user_info.shareTimes,
              answerTime: res.data.user_info.answerTime,
            })
            var cardList = res.data.user_info.cardList
            if (cardList[0] == 1 && cardList[1] == 1 && cardList[2] == 1 && cardList[3] == 1 && cardList[4] == 1 && cardList[5] == 1 && cardList[6] == 1 && cardList[7] == 1 && cardList[8] == 1) {
              that.setData({
                add: 1
              })
            }

          } else {
            if (res.data.msg == "获取卡到上限") {
              that.setData({
                add: 1
              })
            }
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }

        }
      })
    } else {
      that.setData({
        fixeddh: 1
      })
    }

  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    wx.setStorageSync('name', e.detail.value)
    console.log(this.data.name)
  },
  next() {
    var that = this;
    wx.navigateTo({
      url: '../choujiang/choujiang',
    })

  },

  //答题
  toAnswer() {
    var that = this;
    var phone = wx.getStorageSync('phone')
    if (phone) {
      var oldDate = that.data.answerTime;
      if (oldDate) {
        var arr = [];
        arr = oldDate.split('-');
        var myDate = new Date();
        var oldmonth = parseInt(arr[1])
        var oldday = parseInt(arr[2])
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        if (month > oldmonth) {
          var that = this;
          var openId = wx.getStorageSync('openId')
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/answer_time',
            method: 'POST',
            data: {
              openId: openId
            },
            success: res => {
              console.log(res);
              wx.showToast({
                title: '领取成功！',
              })
            }
          })
        } else {
          if (day > oldday) {
            console.log('day增加 可答题')
            var openId = wx.getStorageSync('openId')
            wx.request({
              url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/answer_time',
              method: 'POST',
              data: {
                openId: openId
              },
              success: res => {
                console.log(res);
                wx.showToast({
                  title: '领取成功！',
                })
                setTimeout(() => {
                  wx.redirectTo({
                    url: '../go/go',
                  })
                }, 1000);
              }
            })
          } else {
            console.log('不可答题')
            wx.showToast({
              title: '今天已经领过了',
            })
            setTimeout(() => {
              wx.redirectTo({
                url: '../go/go',
              })
            }, 1000);
          }
        }
      } else {
        var openId = wx.getStorageSync('openId')
        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/answer_time',
          method: 'POST',
          data: {
            openId: openId
          },
          success: res => {
            console.log(res.data.msg);
            wx.showToast({
              title: '领取成功！',
            })
            setTimeout(() => {
              wx.redirectTo({
                url: '../go/go',
              })
            }, 1000);
          }
        })
      }

    } else {
      that.setData({
        fixeddh: 1
      })
    }


  },

  //签到
  toSign() {
    var that = this;
    var phone = wx.getStorageSync('phone')
    phone ? that.setData({
      isBindSign: 1
    }) : that.setData({
      fixeddh: 1
    })
  },
  closeIt() {
    var that = this;
    that.setData({
      isBindSign: !1
    })
  },
  lottery() {
    var that = this;
    var oldDate = that.data.signTime;
    if (oldDate) {
      var arr = [];
      arr = oldDate.split('-');
      var myDate = new Date();
      var oldmonth = parseInt(arr[1])
      var oldday = parseInt(arr[2])
      var month = myDate.getMonth() + 1;
      var day = myDate.getDate();
      if (month > oldmonth) {
        console.log('month增加 可签到')
        that.sign()
      } else {
        if (day > oldday) {
          console.log('day增加 可签到')
          that.sign()
        } else {
          console.log('不可签到')
          that.setData({
            signtext: '今天已经签过了'
          })
        }
      }
    } else {
      that.sign()
    }

  },
  sign() {
    var that = this;
    wx.request({
      url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/get_chance',
      method: 'POST',
      data: {
        openId: that.data.openId
      },
      success: res => {
        console.log(res);
        that.setData({
          openId: res.data.user_info.openId,
          cardList: res.data.user_info.cardList,
          cardNum: res.data.user_info.cardNum,
          signNum: res.data.user_info.signNum,
          signTime: res.data.user_info.signTime,
          times: res.data.user_info.times,
          shareTimes: res.data.user_info.shareTimes,
          answerTime: res.data.user_info.answerTime,
        })
      }
    })
  },
  //刷新
  onPullDownRefresh: function () {
    var that = this;
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
    wx.login({
      success(res) {
        console.log(res)
        if (res.code) {
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/wx_login',
            method: 'POST',
            data: {
              code: res.code,
              p_open_id: that.data.p_open_id
            },
            success: res => {
              wx.setStorageSync('openId', res.data.user_info.openId)
              console.log(res.data.user_info);
              if (res.data.user_info.times) {
                that.setData({
                  times: res.data.user_info.times
                })
              } else {
                that.setData({
                  times: 0
                })
              }
              that.setData({
                p_open_id: res.data.user_info.openId,
                openId: res.data.user_info.openId,
                cardList: res.data.user_info.cardList,
                cardNum: res.data.user_info.cardNum,
                signNum: res.data.user_info.signNum,
                signTime: res.data.user_info.signTime,
                shareTimes: res.data.user_info.shareTimes,
                answerTime: res.data.user_info.answerTime,
              })
              wx.hideNavigationBarLoading(); //完成停止加载图标
              wx.stopPullDownRefresh();
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })


  },


  getPhoneNumber(e) {
    var that = this;
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    wx.request({
      url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/lotteryjn/get_phone_num',
      method: 'POST',
      data: {
        openId: that.data.openId,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData,
      },
      success: res => {
        console.log(res);
        wx.setStorageSync('phone', res.data.mobile)
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/choujiang/register?actid=39300&callback=?', //接收url
          data: {
            phone: res.data.mobile,
            fenxiao: that.data.area,
            geneal: that.data.geneal,
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            that.setData({
              fixeddh: !1
            })
          }
        });
        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
          method: 'POST',
          data: {
            activeId: 30,
            mobile: res.data.mobile,
            areaCode: that.data.area,
            agent: that.data.geneal,
            pageId: that.data.page
          },
          success: res => {
            console.log(res);
          }
        })

      }
    })
  },
  /**右上角分享*/
  onShareAppMessage: function () {
    var that = this;
    console.log(that.data.p_open_id)
    return {
      title: '2022山东中公‘端午放粽嗨’',
      path: 'pages/index/index?p_open_id=' + that.data.p_open_id + '&area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    var that = this;
    console.log(that.data.p_open_id)
    return {
      title: '2022山东中公‘端午放粽嗨’',
      path: 'pages/index/index?p_open_id=' + that.data.p_open_id + '&area=' + that.data.area + '&agent=' + that.data.geneal + '&page=' + that.data.page
    }

  }
})